package com.mineoj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mineoj.common.ErrorCode;
import com.mineoj.exception.BusinessException;
import com.mineoj.model.entity.Solution;
import com.mineoj.model.entity.SolutionThumb;
import com.mineoj.model.entity.SolutionThumb;
import com.mineoj.model.entity.User;
import com.mineoj.service.SolutionService;
import com.mineoj.service.SolutionThumbService;
import com.mineoj.service.SolutionThumbService;
import com.mineoj.mapper.SolutionThumbMapper;
import org.springframework.aop.framework.AopContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
* @author 明月
* @description 针对表【solution_thumb(题解点赞)】的数据库操作Service实现
* @createDate 2025-04-05 16:15:50
*/
@Service
public class SolutionThumbServiceImpl extends ServiceImpl<SolutionThumbMapper, SolutionThumb>
    implements SolutionThumbService{

    @Resource
    private SolutionService solutionService;

    /**
     * 点赞
     *
     * @param solutionId
     * @param loginUser
     * @return
     */
    @Override
    public int doSolutionThumb(long solutionId, User loginUser) {
        // 判断实体是否存在，根据类别获取实体
        Solution solution = solutionService.getById(solutionId);
        if (solution == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        // 是否已点赞
        long userId = loginUser.getId();
        // 每个用户串行点赞
        // 锁必须要包裹住事务方法
        SolutionThumbService solutionThumbService = (SolutionThumbService) AopContext.currentProxy();
        synchronized (String.valueOf(userId).intern()) {
            return solutionThumbService.doSolutionThumbInner(userId, solutionId);
        }
    }

    /**
     * 封装了事务的方法
     *
     * @param userId
     * @param solutionId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int doSolutionThumbInner(long userId, long solutionId) {
        SolutionThumb solutionThumb = new SolutionThumb();
        solutionThumb.setUserId(userId);
        solutionThumb.setSolutionId(solutionId);
        QueryWrapper<SolutionThumb> thumbQueryWrapper = new QueryWrapper<>(solutionThumb);
        SolutionThumb oldSolutionThumb = this.getOne(thumbQueryWrapper);
        boolean result;
        // 已点赞
        if (oldSolutionThumb != null) {
            result = this.remove(thumbQueryWrapper);
            if (result) {
                // 点赞数 - 1
                result = solutionService.update()
                        .eq("id", solutionId)
                        .gt("thumbNum", 0)
                        .setSql("thumbNum = thumbNum - 1")
                        .update();
                return result ? -1 : 0;
            } else {
                throw new BusinessException(ErrorCode.SYSTEM_ERROR);
            }
        } else {
            // 未点赞
            result = this.save(solutionThumb);
            if (result) {
                // 点赞数 + 1
                result = solutionService.update()
                        .eq("id", solutionId)
                        .setSql("thumbNum = thumbNum + 1")
                        .update();
                return result ? 1 : 0;
            } else {
                throw new BusinessException(ErrorCode.SYSTEM_ERROR);
            }
        }
    }
    

}




